var app = getApp(), api = app.api;

Page({
    data: {
        article_list: [],
        no_more: !1,
        loading: !1,
        page: 1,
        article_num: 0,
        views_num: 0,
        mch_id: 0,
    },

    onLoad: function(t) {
        getApp().page.onLoad(this, t), this.setData({
            mch_id: t.mch_id || 0
        });
        var a = this;
        a.loadArticle();
    },

    loadArticle: function() {
        var o = this, s = o.data.page, c = o.data.article_list;
        o.data.no_more ? "function" == typeof t && t() : o.data.loading || (o.setData({
            loading: !0
        }),  getApp().core.showLoading({
            title: '加载中'
        }), getApp().request({
            url: api.default.article_list,
            data: {
                page: s,
                mch_id: o.data.mch_id
            },
            success: function(t) {
                if (console.log(t.data), 0 == t.data.list.length) o.setData({
                    no_more: !0
                }); else {
                    for (var e in t.data.list) {
                        t.data.list[e].img = t.data.list[e].img.split(","),  t.data.list[e].content = t.data.list[e].content.replace("↵", " "),
                        4 < t.data.list[e].img.length && (t.data.list[e].img_length = Number(t.data.list[e].img.length) - 4)
                        4 <= t.data.list[e].img.length ? t.data.list[e].img1 = t.data.list[e].img.slice(0, 4) : t.data.list[e].img1 = t.data.list[e].img;
                    }
                    c = c.concat(t.data.list), o.setData({
                        no_more: !1,
                        page: s + 1,
                        article_num: t.data.row_count,
                        article_list: c
                    })
                }
            },
            complete: function() {
                getApp().core.hideLoading(), o.setData({
                    loading: !1
                });
            }
        }));
    },

    previewImage: function(t) {
        console.log(t);
        var e = t.currentTarget.dataset.id, n = [], i = t.currentTarget.dataset.inde, r = this.data.article_list;
        for (var o in r) if (r[o].id == e) {
            var s = r[o].img;
            for (var c in s) n.push(s[c]);
            wx.previewImage({
                current: s[i],
                urls: n
            });
        }
    },

    onReachBottom: function() {
        this.loadArticle();
    },

    zanArticle: function(t) {
        console.log(t);
        var a = this, n = a.data.article_list, i = t.currentTarget.dataset.id, e = (Number(t.currentTarget.dataset.num));
        app.request({
            url: api.default.article_like,
            cachetime: "0",
            data: {
                id: i
            },
            success: function(t) {
               1 == t.code ? app.core.showModal({
                   title: '提示',
                   content: t.msg,
                   showCancel: !1
               }) : (n[e].givelike = Number(n[e].givelike) + 1, a.setData({
                   article_list: n
               }));
            }
        })
    },

    see: function(t) {
        var e = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "../article-detail/article-detail?id=" + e,
            success: function(t) {},
            fail: function(t) {},
            complete: function(t) {}
        });
    },
    gomch: function(t) {
        var a = this, o = t.currentTarget.dataset.mch_id;
        app.core.navigateTo({
            url: '/mch/shop/shop?mch_id=' + o
        })
    },
    phone: function(t) {
        console.log(t), wx.makePhoneCall({
          phoneNumber: t.currentTarget.dataset.tel,
        })
    }

});
